package com.shujia.flink.window

import org.apache.flink.streaming.api.TimeCharacteristic
import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.assigners.{SlidingProcessingTimeWindows, TumblingEventTimeWindows, TumblingProcessingTimeWindows}
import org.apache.flink.streaming.api.windowing.time.Time

object Demo1TimeWindow {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    val wordsDS: DataStream[(String, Int)] = linesDS.flatMap(_.split(",")).map((_, 1))


    /**
      * TumblingProcessingTimeWindows: 滚动处理时间窗口
      * TumblingEventTimeWindows： 滚动的事件时间窗口, 需要设置时间模式为事件时间，还需要设置时间字段和水位线
      *
      * SlidingProcessingTimeWindows: 滑动的处理时间窗口
      * SlidingEventTimeWindows： 滑动的事件事件窗口，需要设置时间模式为事件时间，还需要设置时间字段和水位线
      */

    val countDS: DataStream[(String, Int)] = wordsDS
      .keyBy(_._1)

      //简写
      //.timeWindow(Time.seconds(15), Time.seconds(5))

      .window(SlidingProcessingTimeWindows.of(Time.seconds(15), Time.seconds(5)))

      .sum(1)

    countDS.print()


    env.execute()


  }
}
